package com.zzit.vjv.controller.documentReview_controller.developer;

import com.zzit.vjv.biz.documentReview_biz.developer.DevListBiz;
import com.zzit.vjv.entity.documentReview_entity.*;
import com.zzit.vjv.util.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("devListController")
@RequestMapping("dev")
public class DevListController {


    @Resource(name = "devListBiz")
    private DevListBiz devListBiz;


    @RequestMapping("/devList")
    public String devListMessage(Model model, @RequestParam(value = "querySoftwareName", required = false) String querySoftwareName, @RequestParam(value = "queryStatus", required = false) Integer queryStatus, @RequestParam(value = "queryFlatformId", required = false) Integer queryFlatformId, @RequestParam(value = "queryCategoryLevel1", required = false) Integer queryCategoryLevel1, @RequestParam(value = "queryCategoryLevel2", required = false) Integer queryCategoryLevel2, @RequestParam(value = "queryCategoryLevel3", required = false) Integer queryCategoryLevel3 , @RequestParam(value = "pageIndex", required = false) Integer pageIndex, HttpSession session) {


        //一级联动
        List<AppCategory> appCategoryList = devListBiz.findByParentIdAppCategoryList(null);

        //app状态
        List<DataDictionary> dataDictionaryList = devListBiz.findDataDictionary(null);

        //所属平台
        List<DataDictionary> dataDictionaryList1 = devListBiz.findDataDictionary("APP_FLATFORM");

        //用于sql条件判断
        Map<String, Object> map = new HashMap<>();

        //用于将前端页面值加入sql作为条件
        map.put("querySoftwareName", querySoftwareName);
        map.put("queryStatus", queryStatus);
        map.put("queryFlatformId", queryFlatformId);
        map.put("queryCategoryLevel1", queryCategoryLevel1);
        map.put("queryCategoryLevel2", queryCategoryLevel2);
        map.put("queryCategoryLevel3", queryCategoryLevel3);

        //用于分页的总条数
        List<AppInfo_result> appInfo_resultList1 = devListBiz.findAppInfo_resultList(map.size() > 0 ? map : null);

        //分页帮助类
        Page page=new Page(appInfo_resultList1.size(),3,pageIndex == null ? 1 : pageIndex);

        //用于分页查询
        map.put("CurrentPageNo", (page.getCurrentPageNo() - 1) * page.getPageSize());
        map.put("PageSize", page.getPageSize());

        //向页面传输分页
        model.addAttribute("pages",page);




        //app信息列表
        List<AppInfo_result> appInfo_resultList = devListBiz.findAppInfo_resultList(map.size() > 0 ? map : null);

        //一级联动
        model.addAttribute("categoryLevel1List", appCategoryList);
        //app状态
        model.addAttribute("statusList", dataDictionaryList);
        //所属平台
        model.addAttribute("flatFormList", dataDictionaryList1);
        //app信息列表
        model.addAttribute("appInfoList", appInfo_resultList);

        //app状态以及/所属平台刷新不变/软件名称
        model.addAttribute("queryStatus", queryStatus);
        model.addAttribute("queryFlatformId", queryFlatformId);
        model.addAttribute("querySoftwareName", querySoftwareName);

        //三级联动刷新不变id
        model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
        //二级联动刷新不变
        List<AppCategory> appCategoryList2 = devListBiz.findByParentIdAppCategoryList(queryCategoryLevel1);
        //三级联动刷新不变
        List<AppCategory> appCategoryList3 = devListBiz.findByParentIdAppCategoryList(queryCategoryLevel2);

        model.addAttribute("categoryLevel2List", appCategoryList2);
        model.addAttribute("categoryLevel3List", appCategoryList3);




        System.out.println("名字" + querySoftwareName + "状态:" + queryStatus + "平台" + queryFlatformId);


        return "jsp/developer/appinfolist";
    }

    /**
     * 二级联动
     *
     * @param pid
     * @return
     */
    @RequestMapping("/listJson1")
    @ResponseBody
    public List<AppCategory> jsonList1(Integer pid) {


        List<AppCategory> appCategoryList1 = devListBiz.findByParentIdAppCategoryList(pid);


        return appCategoryList1;
    }

    /**
     * 二级联动
     *
     * @param pid
     * @return
     */
    @RequestMapping("/listJson2")
    @ResponseBody
    public List<AppCategory> jsonList2(Integer pid) {


        List<AppCategory> appCategoryList2 = devListBiz.findByParentIdAppCategoryList(pid);


        return appCategoryList2;
    }


    /**
     * 用于加载前台添加页面的所属平台
     * @param tcode
     * @return
     */
    @RequestMapping("/addFlatformListJson")
    @ResponseBody
    public List<DataDictionary> addFlatformListJson(String tcode){

        //所属平台
        List<DataDictionary> dataDictionaryList1 = devListBiz.findDataDictionary("APP_FLATFORM");


        return dataDictionaryList1;


    }

    /**
     * 用于加载前台add添加页面的三级联动
     * @param pid 父级节点
     * @return
     */
    @RequestMapping("/addAppCategoryListJson")
    @ResponseBody
    public  List<AppCategory> addAppCategoryListJson(Integer pid){

        List<AppCategory> appCategoryList1 = devListBiz.findByParentIdAppCategoryList(pid);

        return appCategoryList1;
    }


    /**
     * 前台新增app页面(用于判断apk名称)
     * @param APKName1
     * @return
     */
    @RequestMapping("/addAppInfoAPKNameJson")
    @ResponseBody
    public String addAppInfoAPKNameJson(String APKName1){

        String APKName="";
        APKName1=APKName1.replace(" ","");

        if(APKName1.equals("")||APKName1==null){

            System.out.println("请求值"+APKName1);

            APKName= "empty";

        }else {

            System.out.println("请求值" + APKName1);

            if (devListBiz.AppInfoAPKNameByAPKNameFind(APKName1) != null) {

                APKName = "exist";

            } else {
                APKName = "noexist";
            }

        }


        System.out.println(APKName);




        return APKName;
    }


    /**
     * 显示前台新增版本页面,历史版本列表
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/OldAppVersionList")
    public String OldAppVersionList(Model model,Integer id){


        System.out.println("值"+id);

        List<OldAppVersion> oldAppVersionList=devListBiz.OldAppVersionList(id);

        model.addAttribute("appVersionList",oldAppVersionList);


        return "jsp/developer/appversionadd";
    }


    /**
     * 查看app基础信息
     * @return
     */
    @RequestMapping("/appview")
    public String appview(Model model,Integer aid){


        System.out.println("aid"+aid);

        AppInfo_result appInfo_result=devListBiz.appview(aid);


        List<OldAppVersion> oldAppVersionList=devListBiz.OldAppVersionList(aid);

        model.addAttribute("appInfo",appInfo_result);

        model.addAttribute("appVersionList",oldAppVersionList);




        return "jsp/developer/appinfoview";
    }


    /**
     * 修改app信息页面(用于跳转一下)
     * @return
     */
    @RequestMapping("/updAppInfomodify")
    public String updAppInfomodify(Model model,Integer id){


        System.out.println("跳转"+id);

        model.addAttribute("aId",id);


        return "jsp/developer/appinfomodify";
    }


    @RequestMapping("/oldUpdVersionList")
    public String oldUpdVersionList(Model model,Integer vid,Integer aid){

        System.err.println("vid"+vid+"aid"+aid);

        List<OldAppVersion> oldAppVersionList=devListBiz.OldAppVersionList(aid);

        AppVersion appVersion=devListBiz.findAppVersionById(vid);

        model.addAttribute("appVersionList",oldAppVersionList);
        model.addAttribute("appVersion",appVersion);


        return "jsp/developer/appversionmodify";

    }

}
